﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using WINBASE.Utils.DBHelper;

namespace WINBASE
{
    public partial class frmFmmf : Form
    {
        public frmFmmf()
        {
            InitializeComponent();
            this.Text = Application.ProductName + " (v." + Application.ProductVersion + ")";
        }

        protected override void OnLoad(EventArgs e)
        {
            InitializeUI();
            InitializeData();
            base.OnLoad(e);
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        public void InitializeUI()
        {
            this.Size = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
            this.Location = new Point(0, 0);

            dpnlMain.DockLeftPortion = 230;

            ApplicationObject.FrmModuleList.DockHandler.DockPanel = ApplicationObject.FrmMain.dpnlMain;
            ApplicationObject.FrmModuleList.DockHandler.DockAreas = WeifenLuo.WinFormsUI.Docking.DockAreas.DockLeft;
            ApplicationObject.FrmModuleList.Activate();
        }

        public void InitializeData()
        {
            lblUserName.Text = ApplicationData.UserName;
        }

        private void tspViewMaster_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            ToolStripItem myTspItem = (ToolStripItem)e.ClickedItem;
            DataRowView dr;
            switch (myTspItem.Name)
            {
                case "btnNew":
                    BindingSource bs = new BindingSource();
                    bs.DataSource = ((WLCMainForm)ApplicationObject.ActiveForm).DataGrid.DataSource;
                    dr = (DataRowView)bs.AddNew();
                    if (dr["Status Code"] != null)
                    {
                        dr["Status Code"] = "USE";
                    }
                    ApplicationObject.FrmModuleList.ShowDetailForm(dr);
                    break;

                case "btnEdit":
                    if (((WLCMainForm)ApplicationObject.ActiveForm).DataGrid != null)
                    {
                        if (((WLCMainForm)ApplicationObject.ActiveForm).DataGrid.SelectedRows.Count > 0)
                        {
                            dr = (DataRowView)((WLCMainForm)ApplicationObject.ActiveForm).DataGrid.SelectedRows[0].DataBoundItem;
                            ApplicationObject.FrmModuleList.ShowDetailForm(dr);
                        }
                    }
                    break;

                case "btnDelete":
                    if (((WLCMainForm)ApplicationObject.ActiveForm).DataGrid != null)
                    {
                        if (((WLCMainForm)ApplicationObject.ActiveForm).DataGrid.SelectedRows.Count > 0)
                        {
                            dr = (DataRowView)((WLCMainForm)ApplicationObject.ActiveForm).DataGrid.SelectedRows[0].DataBoundItem;
                            if (ApplicationObject.FrmModuleList.DeleteDetail(dr))
                            {
                                dr.Delete();
                            }
                        }
                    }
                    
                    break;
            }
        }

        public void dpnlMain_ActiveDocumentChanged(object sender, EventArgs e)
        {
            if (dpnlMain.ActiveDocument != null)
            {
                if (dpnlMain.ActiveDocument.GetType().BaseType.Name == "WLCMainForm")
                {
                    if (((WLCMainForm)dpnlMain.ActiveDocument).DataGrid != null)
                    {
                        btnNew.Enabled = true;
                        if (((WLCMainForm)dpnlMain.ActiveDocument).DataGrid.Rows.Count > 0)
                        {
                            btnEdit.Enabled = true;
                            btnDelete.Enabled = true;
                        }
                        else
                        {
                            btnEdit.Enabled = false;
                            btnDelete.Enabled = false;
                        }
                        btnRefresh.Enabled = true;
                    }
                    else
                    {
                        btnNew.Enabled = false;
                        btnEdit.Enabled = false;
                        btnDelete.Enabled = false;
                        btnRefresh.Enabled = false;
                    }
                }
                else
                {
                    btnNew.Enabled = false;
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;
                    btnRefresh.Enabled = false;
                }
                ((WLCForm)dpnlMain.ActiveDocument).Refresh();
            }
            else
            {
                btnNew.Enabled = false;
                btnEdit.Enabled = false;
                btnDelete.Enabled = false;
                btnRefresh.Enabled = false;
            }
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}